package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.Subject;
import com.gzxw.mogublog.commons.entity.SubjectItem;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.SubjectMapper;
import com.gzxw.mogublog.xo.service.SubjectItemService;
import com.gzxw.mogublog.xo.service.SubjectService;
import com.gzxw.mogublog.xo.vo.SubjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectItemService subjectItemService;

    @Override
    public IPage<Subject> getList(SubjectVO subjectVO) {

        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(subjectVO.getSubjectName())) {
            subjectQueryWrapper.like("subject_name", subjectVO.getSubjectName().trim());
        }

        subjectQueryWrapper.eq("status", 1);
        subjectQueryWrapper.orderByDesc("sort");
        Page<Subject> page = new Page<>();
        page.setCurrent(subjectVO.getCurrentPage());
        page.setSize(subjectVO.getPageSize());

        IPage<Subject> pageList = this.page(page, subjectQueryWrapper);

        // 1.TODO 获取图片展示
        return pageList;
    }

    @Override
    public String add(SubjectVO subjectVO) {

        Subject subject = new Subject();
        // 先判断该专题是否存在
        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("subject_name", subjectVO.getSubjectName());
        subjectQueryWrapper.eq("status", 1);
        int count = this.count(subjectQueryWrapper);
        if (count > 0) {
            return "该专题已经存在";
        }

        // TODO 加入图片

        subject.setSubjectName(subjectVO.getSubjectName());
        subject.setSummary(subjectVO.getSummary());
        subject.setFileUid(subjectVO.getFileUid());
        subject.setSort(0);
        subject.setStatus(1);
        // 插入专题
        this.save(subject);
        return "插入成功";
    }

    @Override
    public String edit(SubjectVO subjectVO) {

        // 获取当前编辑的专题实体
        Subject subject = this.getById(subjectVO.getUid());

        // 判断当前专题名是否存在
        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("status", 1);
        subjectQueryWrapper.eq("subject_name", subjectVO.getSubjectName().trim());
        Subject preSubject = this.getOne(subjectQueryWrapper);

        if (preSubject != null) {
            return "该专题已经存在";
        }

        subject.setSubjectName(subjectVO.getSubjectName().trim());
        subject.setStatus(1);
        subject.setSummary(subjectVO.getSummary());
        subject.setSort(subjectVO.getSort());
        subject.setUpdateTime(new Date());
        subject.setClickCount(subjectVO.getClickCount());
        subject.setCollectCount(subject.getCollectCount());

        this.updateById(subject);

        return "更新成功";
    }

    @Override
    public String deleteBatch(List<SubjectVO> subjectVOList) {
        if (subjectVOList.size() <= 0) {
            return "参数输入有误";
        }

        // 两个注意点：1.逻辑删除 2.删除判断专题下是否有博客
        List<String> uids = new ArrayList<>();
        subjectVOList.forEach(item -> {
            uids.add(item.getUid());

        });

        // 判断要删除的分类，是否有资源
        QueryWrapper<SubjectItem> subjectItemQueryWrapper = new QueryWrapper<>();
        subjectItemQueryWrapper.eq("status", 1);
        subjectItemQueryWrapper.in("subject_uid", uids);

        int count = subjectItemService.count(subjectItemQueryWrapper);
        if (count > 0) {
            return "该专题下还有博客!";
        }
        // 查询要删除的专题
        Collection<Subject> subjectList = this.listByIds(uids);

        subjectList.forEach(item -> {
            item.setStatus(0);
            item.setUpdateTime(new Date());
        });

        boolean deleteBatch = this.updateBatchById(subjectList);

        return deleteBatch ? "删除成功" : "删除失败";
    }
}
